package com.pignest.hotel.ui.selfhelp;

import java.util.Date;
import java.util.List;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Vbox;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.Member;
import com.pignest.hotel.beans.MemberLevel;
import com.pignest.hotel.beans.TbAccounts;
import com.pignest.hotel.definition.OpenRoomType;
import com.pignest.hotel.service.MemberService;
import com.pignest.hotel.service.ReceptionService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.Logic;
import com.pignest.hotel.util.DateUtil;

public class BaseSelfCheckOutVbox extends Vbox implements EventListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 9221316402427964250L;
	private String curLivingId;
	private ReceptionService receptionService;
	//private AuditService auditService;
	private MemberService memberService;
	private Label roomName;
	//private Label livingType;
	private Label openType;

	private Label memberName;
	private Label memberLevel;
	private Label liveTime;

	private Label adjustPrice;
	private Label roomPrice;
	//private Label leaveTime;
	private Label roomType;
	private Label mBalance;
	private Label mIntegral;
	private Label mCredit;
	private Label mCardState;
	
	private Listbox AccoutsListbox;
	private Label hisUsed;
	//private Button selfCheckOutBtn;
	
	private Living curLiving;

	
	private Member curMember;
	public BaseSelfCheckOutVbox(){
		receptionService = Services.getReceptionService();
		memberService= Services.getMemberService();
		addEventListener("onCreate", this);
	}
	@Override
	public void onEvent(Event event) throws Exception {
		if ("onCreate".equals(event.getName()))
		{
			roomName=(Label)getFellow("roomName");
			//livingType=(Label)getFellow("livingType");
			openType=(Label)getFellow("openType");
			memberName=(Label)getFellow("memberName");
			memberLevel=(Label)getFellow("memberLevel");
			liveTime=(Label)getFellow("liveTime");
			adjustPrice =(Label)getFellow("adjustPrice");
			
			roomPrice =(Label)getFellow("roomPrice");
			//leaveTime =(Label)getFellow("leaveTime");
			roomType =(Label)getFellow("roomType");
			mBalance =(Label)getFellow("mBalance");
			mIntegral =(Label)getFellow("mIntegral");
			mCredit =(Label)getFellow("mCredit");
			mCardState =(Label)getFellow("mCardState");
			
			//storage =(Label)getFellow("storage");
			
			AccoutsListbox=(Listbox)getFellow("AccoutsListbox");
			
			hisUsed =(Label)getFellow("hisUsed");
            init();
		}
	}
	public void init(){
		if(this.curLivingId!=null)
		{
			Living l=receptionService.get(Living.class, curLivingId);
			this.setCurLiving(l);
			roomName.setValue(l.getRoom().getName());
			//livingType.setValue(l.getLiveType());
			openType.setValue(OpenRoomType.valueOf(l.getOpenRoomType()).getDisplayName());
			memberName.setValue(l.getMember().getName());
			Member m =l.getMember();
			this.setCurMember(m);
			MemberLevel level =m.getMemberLevel();
		    if(level!=null)
			    memberLevel.setValue(level.getMemberlevel());
		    else
		    	memberLevel.setValue("");
		    roomPrice.setValue(Utils.toPrice(l.getRoomPrice()));
			//leaveTime.setValue(DateUtil.toString(new Date(), DateUtil.HOTEL_DEFAULT_FORMAT));
		    roomType.setValue(l.getRoom().getType().getName());
			mBalance.setValue(Utils.toPrice(m.getBalance()));
			mIntegral.setValue(String.valueOf(m.getIntegral()));
			mCredit.setValue(String.valueOf(m.getCredit()));
			mCardState.setValue(memberService.queryCardState(m));
		    liveTime.setValue(DateUtil.toString(l.getLiveTime()));
		    adjustPrice.setValue(Utils.toPrice(l.getAdjustPrice()));
		    hisUsed.setValue(Utils.toPrice(memberService.countMemberHistoryUsed(m, true)));
		   // Date liveTime =l.getLiveTime();
			/*String sCurTime =DateUtil.currentDateToString(DateUtil.DEFAULT_DATETIME_FORMAT);
			
			String sLiveTime =DateUtil.toString(liveTime, DateUtil.DEFAULT_DATETIME_FORMAT);*/
			
		}
		queryAccount();
	}
	/**
	 * 核对消费详细信息
	 * **/
	public void queryAccount() {
		if (this.getCurLiving() != null) {
			AccoutsListbox.getItems().clear();
			List<TbAccounts> Ass = receptionService.queryAccountsNotDeposit(this.getCurLiving());
			if (Ass == null)
				return;
			for (TbAccounts as : Ass) {
				Listitem listitem = new Listitem();
				listitem.setValue(as.getId());
				listitem.setParent(AccoutsListbox);
				listitem.appendChild(new Listcell(as.getName()));
				listitem.appendChild(new Listcell(Utils.toPrice(as.getPrice())));
				listitem.appendChild(new Listcell(Utils.toPrice(as.getPrice2())));
				listitem.appendChild(new Listcell(String.valueOf(as.getCounts())));
				listitem.appendChild(new Listcell(as.getEndstate()));
				listitem.appendChild(new Listcell(DateUtil.toString(as.getOptime(), DateUtil.HOTEL_DEFAULT_FORMAT)));
				listitem.appendChild(new Listcell(as.getRemark()));
			}
		}
	}
	public Member getCurMember() {
		return curMember;
	}
	public void setCurMember(Member curMember) {
		this.curMember = curMember;
	}

	public Living getCurLiving() {
		return curLiving;
	}
	public void setCurLiving(Living curLiving) {
		this.curLiving = curLiving;
	}
	public String getCurLivingId() {
		return curLivingId;
	}
	public void setCurLivingId(String curLivingId) {
		this.curLivingId = curLivingId;
	}


}
